<template>
	<view>
		<view class="pl25 pr25" v-if="isUserId">
			<view class="pl20 pr20 bb box flex flex-ac" style="margin-bottom: 30rpx;margin-top: 50rpx;">
				<view class="it1 flex flex-ac">
					<text style="font-size: 30rpx;">找不到档案信息？绑定手机号试试</text>
					<button class='login_btn' style="line-height: 1.5;padding-left: 1;background-color: #F73809;font-size: 30rpx;margin-right: 1rpx;" type='primary' open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber">
					  登录
					</button>
				</view>
			</view>
		</view>
		<radio-group @change="radioChange" style="width: 100%;">
		<view class="da-list">
				<view class="da-card" v-for=" (item,index) in personalFileList" :key="item.id" @click="toClickcard(item)" style="position: relative;">
					<view style="display: flex;align-items: center;margin-left: 40rpx;">
						<view style="margin-right: 20rpx;"><radio style="transform:scale(0.8);" color="#2A82E4" :checked="item.id==personalFileId" :value="item.id" @click.stop="" v-if="medDocInfoId!=0"/></view>
						<image style="width: 150rpx;height: 150rpx;border-radius: 50%;margin-right: 40rpx;" :src="item.headImg?item.headImg:avatarUrl"></image>
						<view style="height: 150rpx;display: flex;flex-direction: column;justify-content: space-around;">
							<view style="font-size: 38rpx;font-weight: bold;">{{item.name}}</view>
							<view style="color: #BBBBBB;">
								<text style="margin-right: 15rpx;display: inline-block;">{{item.sex}}</text>
								<text>{{item.age}}</text>
							</view>
						</view>
					</view>
					<view style="height: 100%;display: flex;align-items: center;justify-content: center;padding-right: 40rpx;" @click.stop="tojkdangan(item)">
						<image style="height: 30rpx;" mode="heightFix" class="arrow" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
					</view>
					
					<image src="../../static/images/xx.png"
					style="height: 42rpx;width: 42rpx;border-radius: 50%;
						position: absolute;top:-10rpx;right: -10rpx;" @click.stop="delDangan(item)" v-if="medDocInfoId==0"></image>	
				</view>
		</view>
		</radio-group>
		<view style="width: 100%;position: fixed;bottom: 40rpx;display: flex;align-items: center;justify-content: space-around;">
			<view :class="[medDocInfoId!=0?'add-btn-2':'add-btn']" @click="openaim()" v-if="!isUserId">+新增档案信息</view>
			<view class="add-btn-2"  @click="returnAdvisoryvideo()" v-if="medDocInfoId!=0">完成</view>
		</view>
		<view class="win box flex flex-ac flex-pc" v-if="aimstate" style="z-index: 999;">
			<view class="ml25 mr25 w100 bgfff brund10">
				<view class="ft16 pt30 pb30 ftc mt20">提示</view>
				<view class="ft12  pb30 ftc bb">你未注册电子健康卡信息，请注册</view>
				<view class="rela aim">
					<view class="flex btnaum pt15 pb15 ft16">
						<text class="it1 ftc" @click="cancelaim">取消</text>
						<text class="c999 line"></text>
						<text class="it1 ftc c74a6fd" @click="confirmaim">确定</text>
					</view>
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="message">
		    <uni-popup-message :type="warnType" :message=warnMessage :duration="warnTime"></uni-popup-message>
		</uni-popup>
		
		<view style="position: fixed;bottom: 15%;right: 20rpx;
			width: 120rpx;height: 120rpx;background: #BE965B;
			box-shadow: 0px 0px 22rpx 2rpx #BE965B;border-radius: 50%;
			display: flex;flex-direction: column;align-items: center;justify-content: center;z-index: 999;"
			@click="tohome">
			<image src="../../static/images/sy@2x.png" style="width: 50%;" mode="widthFix"></image>
			<view style="color: #FFFFFF;">首页</view>
		</view>
	</view>
</template>

<script>
	const WxAuth =  require('@/utils/wxlogin');
	export default {
		data() {
			return {
				warnMessage:'错误',//消息内容
				warnType:'warn',//消息类型  success/warn/info/error
				warnTime:1000,//消息提醒延长时间
				isUserId:false,
				userId: null,
				personalFileList: [], //用户档案列表
				personalFileId: 0,
				medDocInfoId:0,
				name: null,
				sex: null,
				age: null,
				avatarUrl: 'http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/user.png',
				aimstate: false,
				current: 0,
				isBack:false
			};
		},
		onLoad(option) {
			if(option.medDocInfoId=='undefined'||option.medDocInfoId==undefined){
				this.medDocInfoId=0
			}else{
				this.medDocInfoId = option.medDocInfoId;
			}
			
			if(option.userId){
				this.userId = option.userId;
				// this.initData(this.userId);
			}else{
				var usrUserId = uni.getStorageSync("usrUserId");
				if(usrUserId){
					this.isUserId = false;
					this.userId=usrUserId;
					// this.initData(this.userId);
				}else{
					this.isUserId = true;
				}
			}
		},
		onShow(){
			WxAuth.onLogin();
			if(this.userId){
				this.initData(this.userId);
			}
		},
		methods: {
			tohome(){
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			
			toClickcard(obj){
				if(this.medDocInfoId !=0 ){
					this.personalFileId = obj.id
					for (let i = 0; i < this.personalFileList.length; i++) {
						if (this.personalFileList[i].id == obj.id) {
							this.name = this.personalFileList[i].name
							this.sex =  this.personalFileList[i].sex
							this.age = this.personalFileList[i].age
							break;
						}
					}
				}else{
					uni.navigateTo({
						url: '/pages2/jkdangan/index?userId='+this.userId+'&personalFileId='+obj.id
					});
				}
			},
			tojkdangan(obj){
				uni.navigateTo({
					url: '/pages2/jkdangan/index?userId='+this.userId+'&personalFileId='+obj.id
				});
			},
			//选择用户档案
			radioChange(evt) {
				this.personalFileId = evt.target.value
				for (let i = 0; i < this.personalFileList.length; i++) {
					if (this.personalFileList[i].id == evt.target.value) {
						this.name = this.personalFileList[i].name
						this.sex =  this.personalFileList[i].sex
						this.age = this.personalFileList[i].age
						break;
					}
				}
			},
			//跳转回视频咨询或者图文咨询页面
			returnAdvisoryvideo() {
				//console.log("携带的:" + this.name)
				//console.log("携带的:" + this.personalFileId)
				if (this.personalFileId == 0) {
					uni.removeStorageSync('keyInfo');
					uni.removeStorageSync('time');
				}
				uni.setStorageSync("personalFileId", this.personalFileId)
				uni.setStorageSync("name", this.name)
				
				for (let i = 0; i < this.personalFileList.length; i++) {
					if (this.personalFileList[i].id == this.personalFileId) {
						this.name = this.personalFileList[i].name
						this.sex =  this.personalFileList[i].sex
						this.age = this.personalFileList[i].age
						break;
					}
				}
				
				
				let pages = getCurrentPages();
				let prevPage = pages[pages.length-2]
				let prevPage4 = pages[pages.length-4]
				if(prevPage4.route=='pages5/advisrymesseg/index'){
					prevPage4.$vm.personalFileId = this.personalFileId
					prevPage4.$vm.consultant = this.name
					prevPage4.$vm.sex = this.sex
					prevPage4.$vm.age = this.age
					uni.navigateBack({
						delta:3
					})
					}else{
						prevPage.$vm.personalFileId = this.personalFileId
						prevPage.$vm.consultant = this.name
						prevPage.$vm.sex = this.sex
						prevPage.$vm.age = this.age
						uni.navigateBack({
							delta:1
						})
					}
				
			},
			openaim() {
				// this.aimstate = !this.aimstate;
				let url=''
				if(this.medDocInfoId!=0){
					url='/pages/healthcard/index?userId=' + this.userId+'&medDocInfoId='+this.medDocInfoId
				}
				else{
					url= '/pages/healthcard/index?userId=' + this.userId
				}
				uni.navigateTo({
					url: url
				});
			},
			confirmaim() {
				// this.openaim();
				uni.navigateTo({
					url: '/pages/healthcard/index?userId=' + this.userId
				});
			},
			cancelaim() {
				this.openaim();
			},
			//登录
			getPhoneNumber(e){
				var that = this;
				if(e.detail.errMsg.indexOf('fail')==-1){
					that.encryptedData = e.detail.encryptedData;
					that.iv = e.detail.iv;
					that.getCode(e.detail.encryptedData,e.detail.iv,this.hspId);
				}
			},
			//获取用户id并存入缓存
			getCode(encryptedData,iv,hspId){
				var that = this;
				WxAuth.checkSession().then(code => {
				  WxAuth.getUserIdByPhone(code,encryptedData,iv,hspId).then(res =>{
						  if(res.success){
							that.warnMessage = '登录成功';
							that.warnType = 'success';
							that.$refs.popup.open();
							that.loginHX(res.result);//登录环信
							that.isUserId = false;
							that.userId = res.result;
							//初始化数据关注医生数量
							that.initData(res.result);
						  }else{
							that.warnMessage = res.errMsg;
							that.$refs.popup.open();
						  }
					  })
				  })
			},
			//登录环信
			loginHX(userId){
				var test_account = userId+'';
				var test_psword = uni.getStorageSync("test_psword")
				uni.setStorageSync("usrUserId",test_account)
				uni.setStorageSync("myUsername",test_account);
				this.$conn.open({
					apiUrl: this.$im.config.apiURL,
					user: test_account,
					pwd: test_psword,
					grant_type: "password",
					appKey: this.$im.config.appkey
				});
			},
			delDangan(obj){
				this.func.$request('wx/deleteUserPersonalById.tz',{usrPersonalId: obj.id},'GET').then(res=>{
					if (res.data.code == "00000") {
						uni.showToast({
							title: '删除档案成功',
							icon: 'success'
						})
						this.initData(this.userId)
					}
				})
			
			},
			getOpenIdByHspId(){
				var that=this
				let code=uni.getStorageSync('code')
				uni.request({
					url: 'https://app.mzmzb.com/application-usrapp/wx/getOpenIdByHspId.tz',
					method: 'get',
					data: {
						code: code,
						userId: this.userId,
						hspId: this.hspId
					},
					success:(res) =>{
					uni.setStorageSync('openId',res.data.errMsg)
					that.openid=res.data.errMsg
					}
				})
			},
			initData(userId) {
				let openid=uni.getStorageSync('openId')
				if(openid&&openid!=''){
					openid=uni.getStorageSync('openId')
				}else{
					this.getOpenIdByHspId()
					openid=this.openid
				}
				this.func.$request('queryUsrPersonalList.tz',{usrUserId: userId},'GET').then(res=>{
					if (res.data.code == "00000") {
						let personalFileList = res.data.result;
						this.personalFileList = personalFileList
					}
				})
			}
		}
	};
</script>

<style lang="less">
	.da-list{
		background-color: #F2F6F6;
		display: flex;
		align-items: center;
		flex-direction: column;
		height: 100%;
		padding-top: 20rpx;
		padding-bottom: 120rpx;
		// overflow-y: auto;
		.da-card{
			margin-bottom: 30rpx;
			width: 90%;
			height: 230rpx;
			background: #FEFEFE;
			border-radius: 6rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
	}
	.add-btn{
		margin: auto;
		height: 88rpx;
		line-height: 88rpx;
		width: 90%;
		background: #BE965B;
		border-radius: 44rpx;
		color: #FFFFFF;
		text-align: center;
	}
	.add-btn-2{
		margin: auto;
		height: 88rpx;
		line-height: 88rpx;
		width: 45%;
		background: #BE965B;
		border-radius: 44rpx;
		color: #FFFFFF;
		text-align: center;
	}
</style>